package faceb.server.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import faceb.server.dao.CommentDao;
import faceb.server.model.AjaxResponse;
import faceb.server.model.Comment;
import faceb.server.model.User;
import faceb.server.util.CharSetUtil;
import faceb.server.util.MyStringUtils;
import faceb.server.util.ServerFinal;
import faceb.server.util.UUIDUtil;

@Controller
public class CommentController {

	@Resource(name = "commentDao")
	private CommentDao commentDao;

	private Gson gson = new Gson();

	@RequestMapping(value = "/comments/public", method = RequestMethod.POST)
	@ResponseBody
	public String comment(HttpServletRequest req,HttpServletResponse resp) {

		AjaxResponse ar = new AjaxResponse();
		String content = req.getParameter("comment");
		String uid = ((User)req.getSession().getAttribute("user")).getU_id();
		String nid = req.getParameter("nid");
		if (MyStringUtils.hasNullValue(content, uid, nid)) {
			return CharSetUtil.getRespBodyStr(gson.toJson(ar));
		}
		
		String cid = UUIDUtil.get32Code();
		long time = System.currentTimeMillis();
		Comment comment =new Comment(cid, nid, uid, time, content);
		int result = commentDao.setComment(comment);
		if(result == 0){
			return CharSetUtil.getRespBodyStr(gson.toJson(ar));
		}
		
		ar.setStatus(ServerFinal.SUCCESS_CODE);
		ar.setInfo(ServerFinal.SUCCESS_INFO);
		 return CharSetUtil.getRespBodyStr(gson.toJson(ar));
		
	}
	
	@RequestMapping("/commentPage")
	private String commentPage(){
		return "commentPage";
	}
}
